import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import User from '../views/users/index.vue'
import Roles from '../views/rights/Roles.vue'
import Rights from '../views/rights/index.vue'
import Goods from '../views/goods/index.vue'
import Categories from '../views/goods/categories.vue'
import Params from '../views/goods/params.vue'
import Add from '../views/goods/add.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    redirect: '/welcome',
    component: Home,
    children: [{
      path: '/welcome',
      component: Welcome
    },
    {
      path: '/users',
      component: User
    },
    {
      path: '/roles',
      component: Roles
    },
    {
      path: '/rights',
      component: Rights
    },
    {
      path: '/goods',
      component: Goods
    },
    {
      path: '/categories',
      component: Categories
    },
    {
      path: '/params',
      component: Params
    },
    {
      path: '/goods/add',
      component: Add
    }]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, form, next) => {
  const token = sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') return next()
    next('/login')
  }
})
export default router
